//
//  RCUpdateNotice.m
//  CPICApp
//
//  Created by sinosoft on 13-4-12.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "RCUpdateNotice.h"
#import "Config.h"
#import "RCAlertViewUtil.h"
#import "NSString+SBJson.h"
#import "NSObject+SBJson.h"
#import "AppDelegate.h"
#import "ViewController.h"

@implementation RCUpdateNotice

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initUpdateNoticeView
{
    self = [super init];
    return self;
}
-(void)setNoticeRequest//向太保移动平台发送请求报文
{
    NSDictionary *dictUserInfo=[[NSUserDefaults standardUserDefaults] dictionaryForKey:DICT_USER_INFO];
    if (dictUserInfo!=nil) {
        if ([dictUserInfo valueForKey:@"UserAccount"]) {
            NSLog(@"%@",[dictUserInfo valueForKey:@"UserAccount"]);
        }
    }
    else
    {
        [self justShowMessage:@"用户还没登录,请登录系统" title:@"注意"];
        return;
    }
    
//    NSString *strRequestCode=@"RequestCode=61901";
    NSMutableDictionary *dicRequest = [NSMutableDictionary dictionary];
//    NSNumber *PageNo=[NSNumber numberWithInt:1];
//    [dicRequest setObject:PageNo forKey:@"pageNo"];
//    NSNumber *PageSize=[NSNumber numberWithInt:10];
//    [dicRequest setObject:PageSize forKey:@"pageSize"];
    [dicRequest setObject:[dictUserInfo valueForKey:@"UserAccount"] forKey:@"userAccount"];
    NSString *strRequestJson=[dicRequest JSONRepresentation];
//    strRequestJson=[NSString stringWithFormat:@"&RequestData=%@",strRequestJson];
//    NSString *strUrl=[NSString stringWithFormat:@"%@%@%@",Server_Url,strRequestCode,strRequestJson];
//    NSString *strUrlEncod = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
//    NSURL *myurl = [NSURL URLWithString:strUrlEncod];//This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
//    NSLog(@"请求报文为:%@",strUrl);
     requestUpdateNotice= [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
    //设置需要POST的数据，这里提交两个数据，A=a&B=b
    [requestUpdateNotice setPostValue:@"63601" forKey:@"RequestCode"];
    [requestUpdateNotice setPostValue:strRequestJson forKey:@"RequestData"];
    
    [requestUpdateNotice setDelegate:self];
    [requestUpdateNotice setDidFailSelector:@selector(ResponseFail:)];
    [requestUpdateNotice setDidFinishSelector:@selector(ResonseSuccess:)];
    [requestUpdateNotice startAsynchronous];
    
//    showProgress = YES;
//    if (showProgress) {
//        NSLog(@"88888");
//        //[NSThread sleepForTimeInterval:1];
//        progress= [[MBProgressHUD alloc]initWithView:self];
//        [self addSubview:progress];
//        [self bringSubviewToFront:progress];
//        progress.labelText = @"查询中...";
//        [progress show:YES];
//        showProgress = NO;
//    };
    
    
}

- (void)ResonseSuccess:(ASIHTTPRequest *)request
{

//    if (!showProgress)
//    {
//        [progress removeFromSuperview];
//        progress = nil;
//    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSLog(@"dictResponse  %@",dictResponse);
//    NSMutableDictionary *dictAppointmentList=[dictResponse objectForKey:@"reminderServiceInfoList"];
    NSString *strNoticeTotalNum;
    if ([dictResponse valueForKey:@"unread"]) {
        strNoticeTotalNum=[[NSString alloc]initWithFormat:@"%@",[dictResponse valueForKey:@"unread"]];
    }
    else
    {
        strNoticeTotalNum=[[NSString alloc]initWithFormat:@"%@",@"0"];
    }
    NSLog(@"您有%@条未读提醒信息",strNoticeTotalNum);
//    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    [delegate.viewController.btnNotice setTitle:[NSString stringWithFormat:@"您有%@条提醒信息",strNoticeTotalNum] forState:UIControlStateNormal];
//    [strNoticeTotalNum release];
    
    if ([[AppDelegate app].unc.topViewController isKindOfClass:[ViewController class]]) {
        ViewController *viewctrl=(ViewController *)[AppDelegate app].unc.topViewController;
        if ([strNoticeTotalNum isEqualToString:@"0"]) {
            [viewctrl.btnNotice setTitle:@"" forState:UIControlStateNormal];
        }
        else
        {
             [viewctrl.btnNotice setTitle:[NSString stringWithFormat:@"您有%@条未读提醒信息",strNoticeTotalNum]forState:UIControlStateNormal];
        }
    }
}

- (void)ResponseFail:(ASIHTTPRequest *)request
{
//    if (!showProgress)
//    {
//        [progress removeFromSuperview];
//        progress = nil;
//    }
    NSLog(@"服务器连接失败!");
    //[self justShowMessage:@"服务器连接失败" title:@"注意"];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void)dealloc
{
    requestUpdateNotice.delegate=nil;
    [super dealloc];
}

@end
